const { HttpRequest, globalData, mfutil } = getApp();
Component({
  mixins: [],
  data: {
    validationCodeLength: 6,
    verifyphoneData: {
      mobile: '',
      code: '',
    },
    mobilePhonewaring: false,
    codewaring: false,
    phonebtnboxbtoom: false,
    time: 60,
    switch: false
  },
  props: {},
  async didMount() {
    await my.getSystemInfo({
      success: async (res) => {
        this.setData({
          screenheight: Number(res.windowHeight)
        })
      }
    })
  },
  didUpdate() { },
  didUnmount() {
    let { timesetInterval } = this.data;
    clearInterval(timesetInterval)
  },
  methods: {
    //  input框记录
    inputRecord(event) {
      let { item } = event.currentTarget.dataset;
      let { value } = event.detail;
      let { codewaring, validationCodeLength, mobilePhonewaring } = this.data;
      if (item == 'verifyphoneData.mobile') {
        let reg = /^[0-9]*$/;
        if (!reg.test(value)) {
          value = value.replace(/[^\d]/g, '');
        }
        if (value.length == 11) {
          let reg = /^[1][3,4,5,7,8,9][0-9]{9}$/;
          if (!reg.test(value)) {
            mobilePhonewaring = true;
          } else {
            mobilePhonewaring = false
          }
        }
      }
      this.setData({
        [item]: value,
        mobilePhonewaring
      })
    },
    inputBlur(event) {
      let { item } = event.currentTarget.dataset;
      let { value } = event.detail;
      let { mobilePhonewaring, formSubData, codewaring, validationCodeLength } = this.data;
      if (item == 'verifyphoneData.mobile') {
        let reg = /^[1][3,4,5,7,8,9][0-9]{9}$/;
        if (!reg.test(value) && value) {
          mobilePhonewaring = true;
        } else {
          mobilePhonewaring = false
        }
      }
      if (item == 'verifyphoneData.code') {
        if (value && value.length < validationCodeLength) {
          codewaring = true;
        } else {
          codewaring = false;
        }
      }
      this.setData({
        mobilePhonewaring,
        [item]: value,
        codewaring,
        phonebtnboxbtoom: false
      })
    },
    async queryCode() {
      // let {switch} = this.data;
      if (this.data.switch) {
        return
      }
      this.setData({
        switch: true
      })
      let { mobilePhonewaring, verifyphoneData, validationCodeLength } = this.data;
      if (mobilePhonewaring) {
        return
      }
      let path = '/vans-family/api/v2.1/user/validationCode/get';
      let bodyObj = {
        "mobile": verifyphoneData.mobile,
        // "validationCodeLength": validationCodeLength,
        // "expirationSeconds": 300
      }
      let res = await HttpRequest(path, 'POST', {}, bodyObj)
      if (res.data) {
        this.setData({
          queryCode: true,
          switch: false
        })
        let timesetInterval = setInterval(() => {
          let { time } = this.data;
          if (time == 0) {
            this.setData({
              queryCode: false
            })
            clearInterval(timesetInterval)
            this.setData({
              time: 60
            })
            return
          }
          time--
          this.setData({
            time
          })
        }, 1000)
        this.setData({
          timesetInterval
        })
        // my.alert({
        //   content: res.data.validationCode
        // });
      } else {
        this.setData({
          switch: false
        })
        my.alert({
          content: '短信发送次数过多，请稍后再试'
        })
      }
    },
    async loginFn() {
      let { mobilePhonewaring, codewaring, verifyphoneData } = this.data;
      if (!verifyphoneData.mobile) {
        this.setData({
          mobilePhonewaring: true
        })
        return
      }
      if (!verifyphoneData.code) {
        this.setData({
          codewaring: true
        })
        return
      }
      if (mobilePhonewaring || codewaring) {
        return
      }
      let path = '/vans-family/api/v2.1/user/validationCode/check';
      let bodyObj = {
        "mobile": verifyphoneData.mobile, //手机号
        "validationCode": verifyphoneData.code //验证码
      }
      let res = await HttpRequest(path, 'POST', {}, bodyObj)
      if (!res.success) {
        my.alert({
          content: res.errorMsg
        })
        // mfutil.launchErrPage(53001)
      } else {
        let memberObj = {
          "param": verifyphoneData.mobile,
          "type": 0,
        }
        let memberpath = '/vans-family/api/v2.1/project/vansFamily/clm/forward/sendjson/get?url=/v1/member/api/tmall/query';
        let memberres = await HttpRequest(memberpath, 'POST', {}, memberObj)
        if (memberres.data.resultCode == '0' && memberres.data.data) {
          if (memberres.data.data.status == '0') {   // 查询结果为非会员，则跳转到必填信息录入页面   0
            console.log('查询结果为非会员，则跳转到必填信息录入页面')
            let options = {
              verifyphone: '1',
              mobile: verifyphoneData.mobile,
            }
            console.log(options, 'optionsoptionsoptionsoptions', this.props)
            this.props.onChangeregisterActive(options)
          }
          if (memberres.data.data.status == '1') {  // 查询结果为本渠道已绑定会员，提示“手机号已注册，请更换手机号”，停留在原页面   1
            my.alert({
              content: '手机号已注册，请更换手机号'
            })
          }
          console.log(globalData, 'aaaaaaaaaaaaaaaaaaaaaaaaaaa66');

          if (memberres.data.data.status == '2') {      // 查询结果其他渠道已绑定会员，调用会员入会接口，成功后进入dashboard页面      2
            let birthYear = new Date(memberres.data.data.birthDate).getFullYear();
            let birthMonth = new Date(memberres.data.data.birthDate).getMonth() + 1;
            let birthDay = new Date(memberres.data.data.birthDate).getDate();
            let registerObj = {
              "mobilePhone": memberres.data.data.mobilePhone,
              "source": 100000020,
              "subSource": 100000501,
              "fullName": memberres.data.data.fullName,
              "genderCode": memberres.data.data.genderCode,
              "birthYear": birthYear,
              "birthMonth": birthMonth,
              "birthDay": birthDay,
              "thirdType": 1,
              "thirdPartyId": globalData.ouid,
              "storeCode": "TMALL"
            }
            console.log(registerObj, 'registerObjregisterObjregisterObjregisterObj')
            let registerpath = `/vans-family/api/v2.1/project/vansFamily/clm/forward?url=/v1/member/api/tmall/register`;
            let registerres = await HttpRequest(registerpath, 'POST', '', registerObj);
            if (registerres.data.resultCode == '0') {
              my.redirectTo({
                url: '/pages/integral-page/integral-page'
              });
            } else {
              my.alert({
                content: registerres.data.resultDesc
              })
              // mfutil.launchErrPage(53001)
            }
          }
        }
        // 查询结果为该会员已注销，提示“手机号已注销，请更换手机号”，停留在原页面
        if (memberres.data.resultCode == 'ECC0034101') {
          my.alert({
            content: '手机号已注销，请更换手机号'
          })
        }
      }
    }
  },
});
